/**
 * 
 */
package org.swing.utility.common.imp;

import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

/**
 * @author lqnhu
 *
 */
public interface MessageConnection {
	/** Returns a message if one is available. */
	public byte[] tryRead();

	/**
	 * Writes message to the channel. If the underlying channel would block,
	 * this will require future calls to tryWrite() when it becomes available
	 * again. See register().
	 * 
	 * @return true if the write blocks and more data needs to be written.
	 */
	public boolean write(byte[] message);

	/**
	 * Attempts to write any buffered data out the connection.
	 * 
	 * @return true if the write blocks and more data needs to be written.
	 */
	public boolean tryWrite();

	/**
	 * Registers the channel's read and write events with selector. On a read,
	 * call tryRead(). On a write, call tryWrite().
	 */
	public SelectionKey register(Selector selector);

	/** Returns the underlying channel for registration with a selector. */
	// TODO: Remove register()?
	public SelectableChannel getChannel();

	/** Closes the underlying channel. */
	public void close();
}
